<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

         <a-col :span="12">
            <a-form-item label="员工" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :disabled="disableSubmit" v-decorator="['partyId', validatorRules.partyId]" dict="Person,name,partyId,typeId = EMPLOYEE" />
            </a-form-item>
            </a-col>
          <a-col :span="24">
            <a-form-item label="调薪原因" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea :disabled="disableSubmit" v-decorator="['description', validatorRules.description]" rows="4" placeholder="请输入调薪原因"/>
            </a-form-item>
            </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea :disabled="disableSubmit" v-decorator="['remark']" rows="4" placeholder="请输入备注"/>
            </a-form-item>
            </a-col>
        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="薪资明细" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :tableName="refKeys[0]"
            :disabled="disableSubmit"
            :loading="hrSalaryConfirmItemTable.loading"
            :columns="hrSalaryConfirmItemTable.columns"
            :dataSource="hrSalaryConfirmItemTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  export default {
    name: 'HrSalaryConfirmModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
      JSearchSelectTag
    },
    data() {
      return {
        disableSubmit: false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          partyId: {
            rules: [
              { required: true, message: '请输入员工!'},
            ]
          },
          description: {
            rules: [
              { required: true, message: '请输入调薪原因!'},
            ]
          },
        },
        refKeys: ['hrSalaryConfirmItem', ],
        tableKeys:['hrSalaryConfirmItem', ],
        activeKey: 'hrSalaryConfirmItem',
        // 薪资明细
        hrSalaryConfirmItemTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '薪资项',
              key: 'salaryTypeId',
              type: FormTypes.select,
              dictCode:"DataDictionary,description,dictionaryCode, dictionaryTypeId = SALARY_TYPE",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '调薪前金额',
              key: 'currentAmount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' },{ pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额!'}],
            },
            {
              title: '调薪后金额',
              key: 'amount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' },{ pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额!'}],
            },
            {
              title: '开始日期',
              key: 'fromDate',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              dataIndex: (text) => {
                return moment(text).format('YYYY-MM-DD')
              },
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '截至日期',
              key: 'thruDate',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              dataIndex: (text) => {
                return moment(text).format('YYYY-MM-DD')
              },
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/online/special/api/createHrSalaryConfirm",
          edit: "/online/special/api/updateHrSalaryConfirm",
          hrSalaryConfirmItem: {
            list: "/online/api/listHrSalaryConfirmItemByHrSalaryConfirmId",
            deleteBatch: "/online/api/deleteHrSalaryConfirmItemByHrSalaryConfirmBatch",
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'partyId','description','remark','createBu','createBy','createDate','statusId')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.hrSalaryConfirmItem.list, params, this.hrSalaryConfirmItemTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          HrSalaryConfirmItem: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        if(msg){
          this.isHandleOk = false;
        }
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'partyId','description','remark','createBu','createBy','createDate','statusId'))
     },

    }
  }
</script>

<style scoped>
</style>
